<?php

/**
 * news actions.
 *
 * @package    iEasy
 * @subpackage news
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class newsActions extends adminBase
{
    public function executeIndex(sfWebRequest $request)
    {
        $navigator = "/backend.php/news/index?";
        $this->navigator = "";
        $this->catalogId = $request->getParameter("catalogId");
        $this->subCatalogId = $request->getParameter("subCatalogId");
        $this->contentTypeId = $request->getParameter("contentTypeId");
        $submitType = $request->getParameter("submitType");
        if ($submitType != null) {
            if ($submitType != "search") {
                $content = $request->getParameter("content");
                if (!empty($content)) {
                    $Ids = explode(" ", $content);
                    for ($i = 0; $i < count($Ids); $i++)
                    {
                        if (!empty($Ids[$i])) {
                            $currentNews = @Doctrine_Core::getTable('News')->find($Ids[$i]);
                            if (!empty($currentNews)) {
                                $currentNews->setModifyTime(date("Y-m-d H:i:s"));
                                switch ($submitType)
                                {
                                    case "delete": //
                                        $currentNews->setStatusId(1006);
                                        $this->modifyNewsFocus($currentNews, "delete");
                                        break;
                                    case "publish":
                                        if ($currentNews->getStatusId() != 1005) {
                                            $currentNews->setPublishTime(date("Y-m-d H:i:s"));
                                            $currentNews->setStatusId(1005); //
                                        }
                                        $this->modifyNewsFocus($currentNews, "publish");
                                        break;
                                    case "cancelPublish":
                                        $currentNews->setModifyTime(date("Y-m-d H:i:s"));
                                        $currentNews->setStatusId(1004); //不审核
                                        $this->modifyNewsFocus($currentNews, "cancelPublish");
                                        break;
                                    case "recommend":
                                        $this->modifyNewsRecommend($currentNews, "add");
                                        $currentNews->setIsRecommendB("1");
                                        break;
                                    case "cancelRecommend":
                                        $this->modifyNewsRecommend($currentNews, "delete");
                                        $currentNews->setIsRecommendB("0");
                                        break;
                                    case "header":
                                        $this->modifyNewsHeader($currentNews, "add");
                                        $currentNews->setIsHeaderB("1");
                                        break;
                                    case "cancelHeader":
                                        $this->modifyNewsHeader($currentNews, "delete");
                                        $currentNews->setIsHeaderB("0");
                                        break;
                                    case "top":
                                        $currentNews->setIsOnTopB("1");
                                        $this->modifyNewsRecommend($currentNews, "top");
                                        $this->modifyNewsFocus($currentNews, "top");
                                        //$this->modifyNewsHeader($currentNews,"top");
                                        break;
                                    case "cancelTop":
                                        $currentNews->setIsOnTopB("0");
                                        $this->modifyNewsRecommend($currentNews, "cancelTop");
                                        $this->modifyNewsFocus($currentNews, "cancelTop");
                                        //$this->modifyNewsHeader($currentNews,"cancelTop");
                                        break;
                                    case "cancelFocus":
                                        $currentNews->setIsFocusB("0");
                                        $this->modifyNewsFocus($currentNews, "delete");
                                        break;
                                }
                                $currentNews->save();
                            }
                        }
                    }
                }
            }
        }
        $this->recordCount = $request->getParameter('recordCount');
        $this->currentPage = $request->getParameter('currentPage');

        if (null == $this->recordCount || "" == $this->recordCount)
            $this->recordCount = 12;
        if (null == $this->currentPage || "" == $this->currentPage)
            $this->currentPage = 1;

        $query = Doctrine_Query::create()->from("News");
        $catalog = null;
        if ($this->catalogId != null) {
            $query = $query->where('catalog_id=' . $this->catalogId);
            $catalog = Doctrine_Core::getTable("Catalog")->find($this->catalogId);
            if ($catalog != null)
                $this->navigator = "<a href='" . $navigator . "catalogId=" . $this->catalogId . "'>" . $catalog->getDescription() . "</a>";
        }

        $contentType = Doctrine_Core::getTable("ContentType")->find($this->contentTypeId);
        if ($contentType != null) {
            if ($this->navigator != "")
                $this->navigator = $this->navigator . " -> ";
            $this->navigator = $this->navigator . "<a href='" . $navigator . "contentTypeId=" . $this->contentTypeId;
            if ($catalog != null)
                $this->navigator = $this->navigator . "&catalogId=" . $catalog->getCatalogId();
            $this->navigator = $this->navigator . "'>" . $contentType->getDescription() . "</a>";
        }
        if ($this->subCatalogId != null) {
            $query = $query->addwhere('sub_catalog_id=' . $this->subCatalogId);
            $subCatalog = Doctrine_Core::getTable("Catalog")->find($this->subCatalogId);
            if ($subCatalog != null) {
                if ($this->navigator != "")
                    $this->navigator = $this->navigator . " -> ";
                if ($catalog == null)
                    $this->navigator = $this->navigator . "<a href='" . $navigator . "?subCatalogId=" . $subCatalog->getCatalogId() . "'>" . $subCatalog->getDescription() . "</a>";
                else
                    $this->navigator = $this->navigator . "<a href='" . $navigator . "?catalogId=" . $catalog->getCatalogId() . "&subCatalogId=" . $subCatalog->getCatalogId() . "'>" . $subCatalog->getDescription() . "</a>";
            }
        }
        $this->searchProperty = $request->getParameter("searchProperty");
        $searchDelete = "false";
        switch ($this->searchProperty)
        {
            case "top":
                $query = $query->andWhere("is_on_top_b=1");
                break;
            case "recommend":
                $query = $query->andWhere("is_recommend_b=1");
                break;
            case "focus":
                $query = $query->andWhere("is_focus_b=1");
                break;
            case "header":
                $query = $query->andWhere("is_header_b=1");
                break;
            case "publish":
                $query = $query->andWhere("status_id=1005");
                break;
            case "delete":
                $searchDelete = "true";
                $query = $query->andWhere("status_id=1006");
                break;
        }
        $this->searchField = $request->getParameter("searchField");
        switch ($this->searchField)
        {
            case "title":
                $query = $query->andWhere("title like '%" . trim($request->getParameter("searchText")) . "%'");
                break;
            case "keyword":
                $query = $query->andWhere("keyword like '%" . trim($request->getParameter("searchText")) . "%'");
                break;
            case "tag":
                $query = $query->andWhere("tag like '%" . trim($request->getParameter("searchText")) . "%'");
                break;
            case "content":
                $query = $query->andWhere("body like '%" . trim($request->getParameter("searchText")) . "%'");
                break;
        }
        $this->searchText = trim($request->getParameter("searchText"));
        if ($this->searchField != null)
            $this->searchStr = $this->searchStr . "searchField=" . $this->searchField;
        else
        {
            if ($submitType != null && $submitType == "search") { //默认搜索标题
                $query = $query->andWhere("title like '%" . trim($request->getParameter("searchText")) . "%'");
                $this->searchStr = $this->searchStr . "&submitType=search";
            }
        }
        if ($this->searchProperty != null) {
            if ($this->searchStr != "")
                $this->searchStr = $this->searchStr . "&searchProperty=" . $this->searchProperty;
            else
                $this->searchStr = $this->searchStr . "searchProperty=" . $this->searchProperty;
        }
        if ($this->searchText != null)
            $this->searchStr = $this->searchStr . "&searchText=" . $this->searchText;
        if ($this->searchStr != "" && substr($this->searchStr, 0, 1) != "&")
            $this->searchStr = "&" . $this->searchStr;

        if ($searchDelete == "false")
            $query = $query->andWhere("status_id != 1006");

        $dql = $query->getDql();
        $sql = $query->getSqlQuery();
        $query = $query->orderBy('publish_time desc,news_id desc');
        $this->pages = new sfDoctrinePager('News', $this->recordCount); //$this->currentPage
        $this->pages->setQuery($query); //$q   数据库查询语句
        $this->pages->setPage($this->currentPage); //
        $this->pages->init();
    }

    public function executeEdit(sfWebRequest $request)
    {
        $news = new News();
        $this->headerCheck = "false";
        $this->focusCheck = "false";
        $this->recommendCheck = "false";
        $this->relatedNews = "";
        $this->relatedPhoto = "";
        $this->relatedVideo = "";
        $this->isOnTop = "false";
        $this->focusId = "";
        $this->test = "";
        if ($request->getParameter("id") == null)
            return $this->renderText("不合法的页面请求。");
        else
        {
            if ($request->getParameter("txtTitle") == null) //加载
            {
                $news = Doctrine_Core::getTable("News")->find($request->getParameter("id"));
                if ($news != null) {

                    //if($news->getIsFocusB() == 1)
                    {
                        $focus = new FocusContent();
                        $query = Doctrine_Core::getTable("FocusContent")
                                ->createQuery()
                                ->where("content_id = ?", $news->getNewsId())
                                ->addWhere("content_type_i=?", 1000);
                        $focus = $query->execute();
                        foreach ($focus as $currentFocue)
                        {
                            $this->focusId = $currentFocue->getFocusContentId();
                            break;
                        }
                    }
                    if ($news->getIsFocusB() == 1)
                        $this->focusCheck = "true";
                    if ($news->getIsHeaderB() == 1)
                        $this->headerCheck = "true";
                    if ($news->getIsRecommendB() == 1)
                        $this->recommendCheck = "true";
                    if ($news->getIsOnTopB() == 1)
                        $this->isOnTop = "true";
                    $this->relatedNews = $this->getRelatedInfo($news->getNewsId(), "news");
                    $this->relatedPhoto = $this->getRelatedInfo($news->getNewsId(), "photo");
                    $this->relatedVideo = $this->getRelatedInfo($news->getNewsId(), "video");
                }
            }
            else //修改
            {
                $createTime = date("Y-m-d H:i:s");
                $news = Doctrine_Core::getTable("News")->find($request->getParameter("txtHideNewsId"));
                $news->setTitle($request->getParameter("txtTitle"));
                if ($request->getParameter("ckHeaderContent") != null)
                    $news->setIsHeaderB("1");
                else
                    $news->setIsHeaderB("0");
                if ($request->getParameter("ckRecommendContent") != null)
                    $news->setIsRecommendB("1");
                else
                    $news->setIsRecommendB("0");
                if ($request->getParameter("ckFocusContent") != null)
                    $news->setIsFocusB("1");
                else
                    $news->setIsFocusB("0");
                if ($request->getParameter("ckTop") == null)
                    $news->setIsOnTopB("0");
                else
                    $news->setIsOnTopB("1");
                $news->setAuthor($request->getParameter("txtAuthor"));
                $body = $request->getParameter("txtBody");
                $pattern = "/<div>\s*<div style=\"page-break-after: always;?\">\s*<span style=\"display: none;?\">&nbsp;<\/span>\s*<\/div>\s*<\/div>/";
                $body = preg_replace($pattern, "<div style='page-break-after: always;'>\r\n	<span style='display: none;'>&nbsp;</span></div>", $body);
                $body = str_replace("flvPlayer", '<img border="0" src="/images/video_frame.jpg">', $body);
                $body = str_replace("?", '？代替', $body);

                $news->setBody($body);

                $news->setBriefTitle($request->getParameter("txtBriefTitle"));
                $brief = str_replace("flvPlayer", "", trim($request->getParameter("txtBrief")));
                if ($brief != "")
                    $news->setBrief($brief);
                else
                    $news->setBrief(utility::getNoHtmlStr($news->getBody(), 0, 100));
                $news->setModifyTime($createTime);
                $news->setIsAuditB($request->getParameter("check"));
                $news->setKeyword($request->getParameter("txtKeyword"));
                $news->setPublishTime($request->getParameter("txtPublishTime"));
                if ($request->getParameter("txtSubmitType") == "publish") {
                    //$news->setPublishTime($request->getParameter("txtPublishTime"));
                    $news->setStatusId(1005);
                }
                $news->setShortComment($request->getParameter("txtShortComment"));
                $news->setSnapshotBig($request->getParameter("txtSnapshot"));
                $news->setSource($request->getParameter("txtSource"));
                $news->setTag($request->getParameter("txtTag"));
                $news->setCatalogId($request->getParameter("sCatalogId"));
                $news->setSubCatalogId($request->getParameter("sContentCatalogId"));
                $news->setHitMaskI($request->getParameter("txtHitMask"));

                $news->save();

                if ($request->getParameter("ckHeaderContent") != null) {
                    $header = new HeaderContent();
                    $header->setCreateTime($createTime);
                    $query = Doctrine_Core::getTable("HeaderContent")->createQuery()
                            ->where("content_id = ?", $news->getNewsId())
                            ->addWhere("content_type_i=?", 1000)
                            ->execute();
                    foreach ($query as $currentHeader)
                    {
                        $header = $currentHeader;
                        break;
                    }
                    $header->setContentId($news->getNewsId());
                    $header->setContentTypeI(1000);
                    $header->setCatalogId($news->getCatalogId());
                    $header->setModifyTime($news->getModifyTime());
                    if ($request->getParameter("ckTop") == null)
                        $header->setIsOnTopB("0");
                    else
                        $header->setIsOnTopB("1");
                    $header->save();
                }
                else
                {
                    $query = Doctrine_Core::getTable("HeaderContent")->createQuery()
                            ->where("content_id = ?", $news->getNewsId())
                            ->addWhere("content_type_i=?", 1000)
                            ->execute();
                    foreach ($query as $currentHeader)
                    {
                        $currentHeader->delete();
                    }
                }
                if ($request->getParameter("ckRecommendContent") != null) {
                    $recommend = new RecommendContent();
                    $recommend->setCreateTime($createTime);
                    $query = Doctrine_Core::getTable("RecommendContent")->createQuery()
                            ->where("content_id = ?", $news->getNewsId())
                            ->addWhere("content_type_i=?", 1000)
                            ->execute();
                    foreach ($query as $currentRecommend)
                    {
                        $recommend = $currentRecommend;
                        break;
                    }
                    $recommend->setContentId($news->getNewsId());
                    $recommend->setCatalogId($news->getCatalogId());
                    $recommend->setContentTypeI(1000);
                    $recommend->setModifyTime($news->getModifyTime());
                    if ($request->getParameter("ckTop") == null)
                        $recommend->setIsOnTopB("0");
                    else
                        $recommend->setIsOnTopB("1");
                    $recommend->save();
                }
                else
                {
                    $query = Doctrine_Core::getTable("RecommendContent")->createQuery()
                            ->where("content_id = ?", $news->getNewsId())
                            ->addWhere("content_type_i=?", 1000)
                            ->execute();
                    foreach ($query as $currentRecommend)
                    {
                        $currentRecommend->delete();
                    }
                }

                if ($request->getParameter("ckFocusContent") != null) {
                    $focus = null;
                    $query = Doctrine_Core::getTable("FocusContent")->createQuery()
                            ->where("content_id = ?", $news->getNewsId())
                            ->addWhere("content_type_i=?", 1000)
                            ->execute();
                    foreach ($query as $currentFocus)
                    {
                        $focus = $currentFocus;
                        break;
                    }
                    if ($focus == null) {
                        $focus = new FocusContent();
                        $focus->setCreateTime($createTime);
                    }
                    $focus->setContentId($news->getNewsId());
                    $focus->setCatalogId($news->getCatalogId());
                    $focus->setContentTypeI(1000);
                    $focus->setSubTitle($request->getParameter("txtFocusTitle"));
                    $focus->setShortWord($request->getParameter("txtFocusBrief"));
                    $focus->setModifyTime($news->getModifyTime());
                    $focus->setFocusPhoto($request->getParameter("txtFocusLocalFile"));
                    $focus->setStatusId($news->getStatusId());
                    if ($request->getParameter("ckTop") == null)
                        $focus->setIsOnTopB("0");
                    else
                        $focus->setIsOnTopB("1");
                    $focus->save();
                }
                else
                {
                    $query = Doctrine_Core::getTable("FocusContent")->createQuery()
                            ->where("content_id = ?", $news->getNewsId())
                            ->addWhere("content_type_i=?", 1000)
                            ->execute();
                    foreach ($query as $currentFocus)
                    {
                        $currentFocus->setStatusId(1006); //
                        $currentFocus->save();
                    }
                }

                //if($this->deleteRelatedContent($news->getNewsId()) >=0)
                {
                    if ($request->getParameter("txtHideRelatedNews") != "")
                        $this->insertRelatedContent($request->getParameter("txtHideRelatedNews"), $news, 1000, "edit");
                    else
                        $this->deleteRelatedContent($news->getNewsId(), 1000);
                    if ($request->getParameter("txtHideRelatedVideo") != "")
                        $this->insertRelatedContent($request->getParameter("txtHideRelatedVideo"), $news, 1001, "edit");
                    else
                        $this->deleteRelatedContent($news->getNewsId(), 10001);
                    if ($request->getParameter("txtHideRelatedPhoto") != "")
                        $this->insertRelatedContent($request->getParameter("txtHideRelatedPhoto"), $news, 1003, "edit");
                    else
                        $this->deleteRelatedContent($news->getNewsId(), 1003);
                }


                $r = Doctrine_Query::create()->select("count(*) as cnt")
                        ->from("News a")
                        ->where("a.publish_time >= '" . $request->getParameter("txtPublishTime") . "'")
                        ->andWhere("status_id != 1006")
                        ->addWhere("a.catalog_id = ?", $news->getCatalogId())
                        ->addWhere("a.sub_catalog_id = ?", $news->getSubCatalogId())
                        ->execute();
                $cnt = ceil($r[0]['cnt'] / 12);

                $url = "news/index?currentPage=" . $cnt . "&catalogId=" . $news->getCatalogId() . "&subCatalogId=" . $news->getSubCatalogId();
                if ($request->getParameter("contentTypeId") != null)
                    $url = $url . "&contentTypeId=" . $request->getParameter("contentTypeId");
                $this->redirect($url);
            }
        }
        $this->news = $news;
    }

    public function executeAdd(sfWebRequest $request)
    {
        //      $ckFinder = new CKFinder();
        //      $ckFinder->BasePath="/ckfinder/";
        //      $this->CKFinder = $ckFinder;
        $this->publishTime = $request->getParameter("txtPublishTime");
        if ($this->publishTime == null)
            $this->publishTime = date("Y-m-d H:i:s");
        if ($request->getParameter("txtTitle") != null) //添加
        {
            //$desFilePath=utility::uploadFile("image","txtSnapshot");;

            $news = new News();
            $news->setTitle($request->getParameter("txtTitle"));
            if ($request->getParameter("ckHeaderContent") != null)
                $news->setIsHeaderB("1");
            else
                $news->setIsHeaderB("0");
            if ($request->getParameter("ckRecommendContent") != null)
                $news->setIsRecommendB("1");
            else
                $news->setIsRecommendB("0");
            if ($request->getParameter("ckFocusContent") != null)
                $news->setIsFocusB("1");
            else
                $news->setIsFocusB("0");
            if ($request->getParameter("ckTop") == null)
                $news->setIsOnTopB("0");
            else
                $news->setIsOnTopB("1");
            $news->setAuthor($request->getParameter("txtAuthor"));

            $body = $request->getParameter("txtBody");
            $pattern = "/<div>\s*<div style=\"page-break-after: always;?\">\s*<span style=\"display: none;?\">&nbsp;<\/span>\s*<\/div>\s*<\/div>/";
            $body = preg_replace($pattern, "<div style='page-break-after: always;'>\r\n	<span style='display: none;'>&nbsp;</span></div>", $body);
            $body = str_replace("?", '？代替', $body);
            $body = str_replace("flvPlayer", '<img border="0" src="/images/video_frame.jpg">', $body);

            $news->setBody($body);

            $news->setBriefTitle($request->getParameter("txtBriefTitle"));
            $brief = str_replace("flvPlayer", "", trim($request->getParameter("txtBrief")));
            if ($brief != "")
                $news->setBrief($brief);
            else
                $news->setBrief(utility::getNoHtmlStr($news->getBody(), 0, 100));
            $news->setCreateTime(date("Y-m-d H:i:s"));
            $news->setModifyTime($news->getCreateTime());
            $news->setIsAuditB($request->getParameter("check"));

            if ($news->getIsAuditB() == "1")
                $news->setStatusId(1000);
            else
                $news->setStatusId(1004);
            $news->setKeyword($request->getParameter("txtKeyword"));
            //if($request->getParameter("txtSubmitType") == "publish")
            $news->setPublishTime($request->getParameter("txtPublishTime"));
            $news->setShortComment($request->getParameter("txtShortComment"));
            $news->setSnapshotBig($request->getParameter("txtSnapshot"));
            $news->setSource($request->getParameter("txtSource"));

            $news->setTag($request->getParameter("txtTag"));
            $news->setCatalogId($request->getParameter("sCatalogId"));
            $news->setSubCatalogId($request->getParameter("sContentCatalogId"));

            $news->save();

            if ($request->getParameter("ckHeaderContent") == "true") {
                $header = new HeaderContent();
                $header->setContentId($news->getNewsId());
                $header->setCatalogId($news->getCatalogId());
                $header->setContentTypeI(1000);
                $header->setCreateTime($news->getCreateTime());
                $header->setModifyTime($news->getCreateTime());
                if ($request->getParameter("ckTop") == null)
                    $header->setIsOnTopB("0");
                else
                    $header->setIsOnTopB("1");
                $header->save();
            }
            if ($request->getParameter("ckRecommendContent") == "true") {
                $recommend = new RecommendContent();
                $recommend->setContentId($news->getNewsId());
                $recommend->setCatalogId($news->getCatalogId());
                $recommend->setContentTypeI(1000);
                $recommend->setCreateTime($news->getCreateTime());
                $recommend->setModifyTime($news->getCreateTime());
                if ($request->getParameter("ckTop") == null)
                    $recommend->setIsOnTopB("0");
                else
                    $recommend->setIsOnTopB("1");
                $recommend->save();
            }

            if ($request->getParameter("ckFocusContent") == "true") {
                $focus = new FocusContent();
                $focus->setContentId($news->getNewsId());
                //$desFilePath = utility::uploadFile("image","txtFocusLocalFile");
                $focus->setCatalogId($news->getCatalogId());
                $focus->setContentTypeI(1000);
                $focus->setSubTitle($request->getParameter("txtFocusTitle"));

                $focus->setShortWord($request->getParameter("txtFocusBrief"));
                $focus->setCreateTime(date("Y-m-d H:i:s"));
                $focus->setModifyTime($focus->getCreateTime());
                $focus->setFocusPhoto($request->getParameter("txtFocusLocalFile"));
                $focus->setStatusId($news->getStatusId());
                if ($request->getParameter("ckTop") == null)
                    $focus->setIsOnTopB("0");
                else
                    $focus->setIsOnTopB("1");
                $focus->save();
            }
            if ($request->getParameter("txtHideRelatedNews") != "")
                $this->insertRelatedContent($request->getParameter("txtHideRelatedNews"), $news, 1000);
            if ($request->getParameter("txtHideRelatedVideo") != "")
                $this->insertRelatedContent($request->getParameter("txtHideRelatedVideo"), $news, 1001);
            if ($request->getParameter("txtHideRelatedPhoto") != "")
                $this->insertRelatedContent($request->getParameter("txtHideRelatedPhoto"), $news, 1003);
            $url = "news/index?catalogId=" . $news->getCatalogId() . "&subCatalogId=" . $news->getSubCatalogId();
            if ($request->getParameter("contentTypeId") != null)
                $url = $url . "&contentTypeId=" . $request->getParameter("contentTypeId");
            $this->redirect($url);
        }
    }

    function insertRelatedContent($relatedStr, $News, $relatedContentTypeId, $type = "add")
    {
        $relatedArray = @explode(";", $relatedStr);
        $oldRelatedContents = Doctrine_Core::getTable("RelatedContent")->createQuery()
                ->where("content_type_i = 1000")
                ->addWhere("content_id=?", $News->getNewsId())
                ->addWhere("r_content_type_i=?", $relatedContentTypeId)
                ->execute();
        //把原来有的但是修改后没有的删除掉
        foreach ($oldRelatedContents as $oldRelatedContent)
        {
            $flag = 0;
            foreach ($relatedArray as $key => $value)
            {
                if ($oldRelatedContent->getRContentId() == $value) {
                    $flag = 1;
                    break;
                }
            }
            if ($flag == 0) {
                //如果是新闻，则需要双向删除
                if ($relatedContentTypeId == 1000) {
                    //要双向删除
                    $tempRelatedContents = Doctrine_Core::getTable("RelatedContent")->createQuery()
                            ->where("content_type_i = 1000")
                            ->addWhere("content_id=?", $oldRelatedContent->getRContentId())
                            ->addWhere("r_content_type_i=1000")
                            ->addWhere("r_content_id=?", $News->getNewsId())
                            ->execute();
                    foreach ($tempRelatedContents as $tempRelatedContent)
                        $tempRelatedContent->delete();
                }

                $oldRelatedContent->delete();
            }
        }

        //插入相关的信息
        foreach ($relatedArray as $key => $value)
        {
            $flag = 0;
            if ($type == "edit") {
                $relatedContentsCount = Doctrine_Core::getTable("RelatedContent")->createQuery()
                        ->where("content_id=?", $News->getNewsId())
                        ->addWhere("content_type_i=1000")
                        ->addWhere("r_content_id=?", $value)
                        ->addWhere("r_content_type_i=?", $relatedContentTypeId)
                        ->execute()->count();
                if ($relatedContentsCount > 0)
                    $flag = 1;

            }
            if ($flag == 0) {
                $relatedContent = new RelatedContent();
                $relatedContent->setCreateTime(date("Y-m-d H:i:s"));
                $relatedContent->setModifyTime($relatedContent->getCreateTime());
                $relatedContent->setContentId($News->getNewsId());
                $relatedContent->setContentTypeI(1000);
                $relatedContent->setRContentId($value);
                $relatedContent->setRContentTypeI($relatedContentTypeId);
                $relatedContent->setCatalogId($News->getCatalogId());
                $relatedContent->save();

                if ($relatedContentTypeId == 1000) //相关新闻是双向的
                {
                    $relatedNews = Doctrine_Core::getTable("News")->find($value);
                    if ($relatedNews != null) {
                        $RerelatedContent = new RelatedContent();
                        $RerelatedContent->setCreateTime(date("Y-m-d H:i:s"));
                        $RerelatedContent->setModifyTime($relatedContent->getCreateTime());
                        $RerelatedContent->setContentId($value);
                        $RerelatedContent->setContentTypeI(1000);
                        $RerelatedContent->setRContentId($News->getNewsId());
                        $RerelatedContent->setRContentTypeI(1000);
                        $RerelatedContent->setCatalogId($relatedNews->getCatalogId());
                        $RerelatedContent->save();
                    }
                }
            }
        }
    }

    function deleteRelatedContent($newsId, $relatedContentTypeId)
    {
        $sqlStr = "delete from RelatedContent where content_type_i =$relatedContentTypeId and r_content_type_i = 1000 and r_content_id = " . $newsId . ";";

        $sqlStr = $sqlStr . "delete from RelatedContent where content_type_i = 1000 and content_id = " . $newsId . " and r_content_type_i =" . $relatedContentTypeId;
        return $this->updateDataRows($sqlStr);
    }

    function getRelatedInfo($newsId, $relatedType)
    {
        $query = Doctrine_Core::getTable("RelatedContent")->createQuery()
                ->where("content_id = ?", $newsId)
                ->addWhere("content_type_i = ?", 1000);
        $relatedStr = "";
        switch ($relatedType)
        {
            case "news":
                $query->addWhere("r_content_type_i = ?", 1000);
                break;
            case "video":
                $query->addWhere("r_content_type_i = ?", 1001);
                break;
            case "photo":
                $query->addWhere("r_content_type_i = ?", 1003);
                break;
        }

        $news = $query->execute();
        foreach ($news as $currentNews)
        {
            //$relatedStr = $relatedStr.$currentNews->getRContentId().";";
            if ($relatedStr == "")
                $relatedStr = $relatedStr . $currentNews->getRContentId();
            else
                $relatedStr = $relatedStr . ";" . $currentNews->getRContentId();
        }
        return $relatedStr;
    }

    function updateDataRows($sqlStr)
    {
        $dbBase = new DbBase();
        return $dbBase->update($sqlStr);
    }

    function modifyNewsRecommend($currentNews, $actionType)
    {
        $recommend = Doctrine_Core::getTable("recommendContent")->createQuery()
                ->where("content_id=?", $currentNews->getNewsId())
                ->andWhere("content_type_i=1000")
                ->execute()->getFirst();
        if ($actionType == "add") {
            if ($recommend == null) {
                $recommend = new RecommendContent();
                $recommend->setContentId($currentNews->getNewsId());
                $recommend->setContentTypeI(1000);
                $recommend->setCreateTime(date("Y-m-d H:i:s"));
                $recommend->setModifyTime(date("Y-m-d H:i:s"));
                $recommend->setCatalogId($currentNews->getCatalogId());
                $recommend->save();
            }
        }
        else
        {
            if ($recommend != null) {
                switch ($actionType)
                {
                    case "top":
                        $recommend->setIsOnTopB("1");
                        $recommend->save();
                        break;
                    case "cancelTop":
                        $recommend->setIsOnTopB("0");
                        $recommend->save();
                        break;
                    case "delete":
                        $recommend->delete();
                        break;
                }
            }
        }
    }

    function modifyNewsHeader($currentNews, $actionType)
    {
        $header = Doctrine_Core::getTable("HeaderContent")->createQuery()
                ->where("content_id=?", $currentNews->getNewsId())
                ->andWhere("content_type_i=1000")
                ->execute()->getFirst();
        if ($actionType == "add") {
            if ($header == null) {
                $header = new HeaderContent();
                $header->setContentId($currentNews->getNewsId());
                $header->setContentTypeI(1000);
                $header->setCreateTime(date("Y-m-d H:i:s"));
                $header->setModifyTime(date("Y-m-d H:i:s"));
                $header->setCatalogId($currentNews->getCatalogId());
                $header->save();
            }
        }
        else
        {
            if ($header != null) {
                switch ($actionType)
                {
                    case "top":
                        $header->setIsOnTopB("1");
                        $header->save();
                        break;
                    case "cancelTop":
                        $header->setIsOnTopB("0");
                        $header->save();
                        break;
                    case "delete":
                        $header->delete();
                        break;
                }
            }
        }
    }

    function modifyNewsFocus($currentNews, $actionType)
    {
        $focus = Doctrine_Core::getTable("FocusContent")->createQuery()
                ->where("content_id=?", $currentNews->getNewsId())
                ->andWhere("content_type_i=1000")
                ->execute()->getFirst();
        if ($focus != null) {
            switch ($actionType)
            {
                case "top":
                    $focus->setIsOnTopB("1");
                    break;
                case "cancelTop":
                    $focus->setIsOnTopB("0");
                    break;
                case "delete":
                    $focus->setStatusId(1006);
                    break;
                case "publish":
                case "cancelPublish":
                    $focus->setStatusId($currentNews->getStatusId());
                    $focus->setCreateTime(date("Y-m-d H:i:s"));
                    break;
            }
            $focus->save();
        }

    }
}
